package core.view
{
	import core.Main;
	
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	
	import org.ais.event.TEvent;

	public class ToolBarView extends VUSprite
	{
		static protected var instance:ToolBarView;
		/**
		 * 背景
		 */
		protected var bgMc:MovieClip;
		/**
		 * 音量
		 */
		protected var _volume:Number = 0.7;
		/**
		 * 是否在播放
		 */
		protected var _isPlaying:Boolean;
		/**
		 * 文件地址
		 */
		protected var _URL:String;
		
		public function ToolBarView()
		{
			init();
		}
		
		/**
		 * 
		 * 初始化
		 * 
		 */
		protected function init():void
		{
			bgMc = new SWF_TOOL_BAR();
			bgMc.mc_volume.buttonMode = true;
			this.addChild(bgMc);
			
			setVolume(_volume);
			
			__addEvent();
		}
		
		/**
		 * 
		 * 注册事件
		 * 
		 */
		protected function __addEvent():void
		{
			bgMc.btn_mc.addEventListener(MouseEvent.CLICK, __btnHandler);
			bgMc.mc_volume.addEventListener(MouseEvent.MOUSE_DOWN, __mcVolumeHandler);
			
			VideoView.getInstance().getNetStream().addEventListener(NetStatusEvent.NET_STATUS, __netStatusHadler);
			
			TEvent.newTrigger("IPLAYER_0", __triggerHandler);
		}
		
		/**
		 * 
		 * 移除事件
		 * 
		 */
		protected function __removeEvent():void
		{
			bgMc.btn_mc.removeEventListener(MouseEvent.CLICK, __btnHandler);
			bgMc.mc_volume.removeEventListener(MouseEvent.MOUSE_DOWN, __mcVolumeHandler);
			
			VideoView.getInstance().getNetStream().removeEventListener(NetStatusEvent.NET_STATUS, __netStatusHadler);
			
			TEvent.removeTrigger("IPLAYER_0", __triggerHandler);
		}
		
		/**
		 * 
		 * 按钮事件侦听
		 * 
		 * @param e
		 * 
		 */
		protected function __btnHandler(e:MouseEvent):void
		{
			var obj:* = e.target;
			switch (obj.name.replace(/btn_/ig, "")) {
				case "play":
				case "pause":
					togglePause();
					break;
				case "stop":
					stop();
					break;
				case "mute0":
				case "mute1":
					mute();
					break;
				case "screen0":
				case "screen1":
					stage.displayState = stage.displayState === StageDisplayState.NORMAL ? StageDisplayState["FULL_SCREEN_INTERACTIVE"] : StageDisplayState.NORMAL;
					break;
			}
		}
		
		/**
		 * 
		 * 设置音量
		 * 
		 * @param e
		 * 
		 */
		protected function __mcVolumeHandler(e:MouseEvent):void
		{
			var obj:* = e.currentTarget;
			var _p:Number = e.localX / obj.width;
			setVolume(_p);
			bgMc.btn_mc.mc_mute.gotoAndStop(1);
			e = null;
		}
		
		/**
		 * 
		 * UNetStreamy 侦听
		 * 
		 * @param e
		 * 
		 */
		public function __netStatusHadler(e:NetStatusEvent):void
		{
			switch (e.info.code) {
				case "NetStream.Play.Stop":
					_isPlaying = false;
					bgMc.btn_mc.mc_play.gotoAndStop(1);
					TEvent.trigger(VideoView.getInstance().getNetStream().NAME, "CLEAR_PROGRESS_ALL");
					break;
			}
		}
		
		/**
		 * 
		 * UNetStreamy 全局侦听
		 * 
		 * @param type
		 * @param data
		 * 
		 */
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case "PLAY":
					play(data);
					break;
				case "STOP":
					stop();
					break;
				case "TOGGLE_PAUSE":
					togglePause();
					break;
				case "MUTE":
					mute();
					break;
				case "SET_URL":
					setURL(data);
					break;
				case "SET_VOLUME":
					setVolume(data);
					break;
			}
			type = null;
			data = null;
		}
		
		/**
		 * 
		 * 播放
		 * 
		 * @param url
		 * 
		 */
		public function play(url:String):void
		{
			if (!url) return;
			url = url.replace(/^\s+|\s+$/g, "");
			if (!url) return;
			if (/^p\:/ig.test(url) === true) {
				TEvent.trigger(VideoView.getInstance().getNetStream().NAME, "PROXY", url);
				return;
			}
			if (/^file/ig.test(url) === true) Main.IS.isLocal = true;
			else Main.IS.isLocal = false;
			_URL = url;
			_isPlaying = true;
			VideoView.getInstance().play(_URL);
			bgMc.btn_mc.mc_play.gotoAndStop(2);
			url = null;
		}
		
		/**
		 * 
		 * 停止
		 * 
		 */
		public function stop():void
		{
			VideoView.getInstance().getNetStream().seekIndex = 0;
			VideoView.getInstance().getNetStream().seekByPoint(0);
			VideoView.getInstance().getNetStream().pause();
			bgMc.btn_mc.mc_play.gotoAndStop(1);
			
			TEvent.trigger(VideoView.getInstance().getNetStream().NAME, "CLEAR_PLAY", 0);
		}
		
		/**
		 * 
		 * 播放/暂停
		 * 
		 */
		public function togglePause():void
		{
			var _cf:int = bgMc.btn_mc.mc_play.currentFrame;
			if (_isPlaying === false) play(_URL);
			else VideoView.getInstance().getNetStream().togglePause();
			bgMc.btn_mc.mc_play.gotoAndStop(_cf === 1 ? 2 : 1);
		}
		
		public function mute():void
		{
			var _cf:int = bgMc.btn_mc.mc_mute.currentFrame;
			if (_cf === 1) {
				var _v:Number = _volume;
				setVolume(0);
				_volume = _v;
			}
			else setVolume(_volume);
			bgMc.btn_mc.mc_mute.gotoAndStop(_cf === 1 ? 2 : 1);
		}
		
		/**
		 * 
		 * 设置 URL
		 * 
		 * @param url
		 * 
		 */
		public function setURL(url:String):void
		{
			_URL = url;
			url = null;
			TEvent.trigger("IPLAYER_0", "GET_URL", url);
		}
		
		/**
		 * 
		 * 设置音量
		 * 
		 * @param p
		 * 
		 */
		public function setVolume(p:Number):void
		{
			_volume = p < 0 ? 0 : p;
			VideoView.getInstance().setVolume(_volume);
			bgMc.mc_volume.mc_mask.width = bgMc.mc_volume.width * (_volume > 1 ? 1 : _volume);
			if (_volume !== 0 && bgMc.btn_mc.mc_mute.currentFrame !== 1) bgMc.btn_mc.mc_mute.gotoAndStop(1);
			TEvent.trigger("IPLAYER_0", "GET_VOLUME", _volume);
		}
		
		/**
		 * 
		 * 重置大小
		 * 
		 * @param w
		 * @param h
		 * 
		 */
		override public function setSize(w:Number, h:Number):void
		{
			var _w:Number = w - bgMc.mc_bg.width;
			bgMc.mc_bg.width = w;
//			bgMc.btn_mc.mc_play.x += _w;
//			bgMc.btn_mc.btn_stop.x += _w;
			
			bgMc.btn_mc.mc_mute.x += _w;
			bgMc.btn_mc.mc_screen.x += _w;
			bgMc.mc_volume.x += _w;
			
			y = h - bgMc.height;
			
			switch (stage.displayState) {
				case StageDisplayState["NORMAL"]:
					bgMc.btn_mc.mc_screen.gotoAndStop(1);
					break;
				default:
					bgMc.btn_mc.mc_screen.gotoAndStop(2);
					break;
			}
		}
		
		/**
		 * 
		 * 清空
		 * 
		 */
		override public function clear():void
		{
			__removeEvent();
			bgMc = null;
			_URL = null;
			instance = null;
			super.clear();
		}
		
		static public function getInstance():ToolBarView
		{
			if (null === instance) instance = new ToolBarView();
			return instance;
		}
		
	}
}